﻿using System;
using System.Linq;
using System.Windows.Forms;

using QuickShot.Shortcut;
using QuickShot.Preferences;

namespace QuickShot.Forms
{
    public partial class PreferencesForm : Form
    {
        public UserPreferences Preferences { get; private set; }

        public PreferencesForm(UserPreferences preferences)
        {
            InitializeComponent();
            Preferences = preferences;
        }

        private void PreferencesForm_Load(object sender, EventArgs e)
        {
            transitionTabControl1.Initialize();

            fullScreenHotkey.Modifier = HotkeyManager.MapKeys(Preferences.FullscreenHotkey.Modifier);
            fullScreenHotkey.Hotkey = Preferences.FullscreenHotkey.Key;
            regionHotkey.Modifier = HotkeyManager.MapKeys(Preferences.RegionHotkey.Modifier);
            regionHotkey.Hotkey = Preferences.RegionHotkey.Key;
            activeWindowHotkey.Modifier = HotkeyManager.MapKeys(Preferences.ActiveWindowHotkey.Modifier);
            activeWindowHotkey.Hotkey = Preferences.ActiveWindowHotkey.Key;

            
            saveAfterCapture.Checked = Preferences.SaveAfterCapture;
            capturePrimaryDispaly.Checked = Preferences.CapturePrimaryDisplay;
            captureAllDispalys.Checked = !Preferences.CapturePrimaryDisplay;
            nameAutomatically.Checked = Preferences.AutomaticNaming;
            openSaveDialogAfterCapture.Checked = !Preferences.AutomaticNaming;
            saveLocation.Text = Preferences.SaveDirectory;

            formatComboBox.SelectedIndex = (int)Preferences.Format;

        }

        private void PreferencesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            UserPreferences p = new UserPreferences()
            {
                FullscreenHotkey = new Hotkey(HotkeyManager.MapModifiers(fullScreenHotkey.Modifier), fullScreenHotkey.Hotkey),
                RegionHotkey = new Hotkey(HotkeyManager.MapModifiers(regionHotkey.Modifier), regionHotkey.Hotkey),
                ActiveWindowHotkey = new Hotkey(HotkeyManager.MapModifiers(activeWindowHotkey.Modifier), activeWindowHotkey.Hotkey),
                CapturePrimaryDisplay = capturePrimaryDispaly.Checked,
                SaveAfterCapture = saveAfterCapture.Checked,
                AutomaticNaming = nameAutomatically.Checked,
                SaveDirectory = saveLocation.Text,
                Format = (ImageFormat)formatComboBox.SelectedIndex
            };

            if (p != Preferences) Preferences = p;

            DialogResult = DialogResult.OK;
        }

        private void aboutButton_Click(object sender, EventArgs e)
        {
            new AboutForm().ShowDialog();
        }

        private void saveLocation_DoubleClick(object sender, EventArgs e)
        {
            using (FolderBrowserDialog fDialog = new FolderBrowserDialog())
            {
                fDialog.Description = "CHOOSE A DIRECTORY TO SAVE YOUR SCREEN CAPTURES TOO...";
                if (fDialog.ShowDialog() == DialogResult.OK)
                    saveLocation.Text = fDialog.SelectedPath;
            }
        }
    }
}
